<?php
/**
 * CAMA Land Use
 * Return CAMA land use information for a parcel.
 * 
 * @param 		string 		$pid 			Parcel ID
 * @param 		string			$format		format of output, either json or xml
 * @return 		string						resulting json or xml string
 */

# Includes
require_once("../../inc/error.inc.php");
require_once("../../inc/database.inc.php");
require_once("../../inc/security.inc.php");

# Set arguments for error email 
$err_user_name = "Tobin";
$err_email = "tobin.bradley@mecklenburgcountync.gov";

# Retrive URL arguments
try {
	$pid = $_REQUEST['pid'];
	$format = $_REQUEST['format'];
} 
catch (Exception $e) {
    trigger_error("Caught Exception: " . $e->getMessage(), E_USER_ERROR);
}

# Performs the query and returns XML or JSON
try {
	$sql = "SELECT id_pid as parcel_id, dbo.ctb_PubLandUse.txt_LandUse_fullDesc as land_use, dbo.tb_Publand.cnt_landunits as units, dbo.tb_publand.txt_neigh_desc as neighborhood, cde_landunittype as land_unit_type 
		FROM dbo.tb_PubLand LEFT JOIN dbo.ctb_PubLandUse ON dbo.tb_PubLand.cde_LandUseCode = dbo.ctb_PubLandUse.cde_LandUse WHERE ";
		
	$sql .= "id_pid = ?";
	$sql = sanitizeSQL($sql);
	$camaconn = camaConnection();

    /*** fetch into an PDOStatement object ***/
    $recordSet = $camaconn->prepare($sql);
    $recordSet->bindParam(1, $pid);
    $recordSet->execute();

	if ($format == 'xml') {
		require_once("../../inc/xml.pdo.inc.php");
		header("Content-Type: text/xml");
		echo rs2xml($recordSet);
	}
	elseif ($format == 'json') {
		require_once("../../inc/json.pdo.inc.php");
		header("Content-Type: application/json");
		echo rs2json($recordSet);
	}
	else {
		trigger_error("Caught Exception: format must be xml or json.", E_USER_ERROR);
	}
}
catch (Exception $e) {
	trigger_error("Caught Exception: " . $e->getMessage(), E_USER_ERROR);
}

?>